package controller;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.SaleManageService;
import dto.ListSalePageDTO;
import dto.MemberDTO;
import dto.ProductDTO;
import dto.SaleListDTO;

@Controller
public class SaleController {
	private SaleManageService service;
	public SaleController(SaleManageService service) {
		this.service = service;
	}
	@RequestMapping("saleRegister.do")
	public ModelAndView saleRegister(SaleListDTO saledto, HttpSession session){
		ModelAndView mv = null;
		MemberDTO member=(MemberDTO) session.getAttribute("memberDTO");
		saledto.setMemberId(member.getId());
		member.setMileage(member.getMileage()+100);			//세션에서 받아온 member에 마일리지+100를 더해 준다.
		member.setCredit_score(member.getCredit_score()+1);
		try{
			service.registSale(saledto);
			service.modifyMemberMileage(member);		//마일리지를 추가한 디비에 넣는다.
			session.setAttribute("memberDTO", member);		//세션도 바꿔준다.
			mv = new ModelAndView("saleSelfInputSuccess.do","saledto",saledto);
		}
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}		
		return mv;
	}
	@RequestMapping("saleModifyForm.do")
	public ModelAndView saleModifyForm(int saleNo){
		ModelAndView mv = null;
		SaleListDTO saleDTO = null;
		try{
			saleDTO = service.selectSaleByNo(saleNo);
			mv = new ModelAndView("saleModifyFormTiles.do", "saleDTO", saleDTO);
		}
		
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("saleDelete.do")
	public ModelAndView saleDelete(int saleNo, HttpSession session){
		ModelAndView mv = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		member.setMileage(member.getMileage()-100);		//삭제시 마일리지 -100을 한다.
		member.setCredit_score(member.getCredit_score()-1);
		try{
			service.delete(saleNo);
			service.modifyMemberMileage(member);				//마일리지를 추가한 디비에 넣는다.
			session.setAttribute("memberDTO", member);		//세션도 바꿔준다.
			mv = new ModelAndView("saleTempTiles.do","message","정상적으로 삭제되었습니다.");
		}
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("saleAllList.do")
	public ModelAndView saleAllList(HttpSession session){
		ModelAndView mv = null;
		ArrayList<SaleListDTO> saleList = new ArrayList<SaleListDTO>();
		try{
			if(session.getAttribute("memberDTO")==null){
				mv = new ModelAndView("saleList.do","message","로그인을 먼저 해주세요.");
			}
			else{
				saleList= service.selectSaleBy(((MemberDTO)session.getAttribute("memberDTO")).getId());
				mv = new ModelAndView("saleList.do","saleList",saleList);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		
		return mv;
	}
	
	@RequestMapping("modifySale.do")
	public ModelAndView saleModify(SaleListDTO saleDTO){
		ModelAndView mv = null;
		try{
			service.modifySale(saleDTO);
			mv = new ModelAndView("saleModifySuccessTiles.do","saledto",saleDTO);
		}
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("deleteFileSale.do")
	public ModelAndView deleteFile(int saleNo){
		ModelAndView mv = null;
		SaleListDTO saledto = null;
		try {
			saledto = service.selectSaleByNo(saleNo);
			service.deleteFile(saledto);
			mv = new ModelAndView("saleModifyFormTiles.do","saleDTO",saledto);
			
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("selectSaleListByPage.do")
	public ModelAndView saleAllListByPaging(HttpServletRequest request){
		ModelAndView mv = null;
		ListSalePageDTO listSaleDTO = null;
		int page=1;
		try{
			if(request.getParameter("page")!= null){
				page = Integer.parseInt(request.getParameter("page"));
			}
			else{
				page = 1;
			}
			listSaleDTO = service.getSaleListByPage(page);
			
			mv = new ModelAndView("saleListPagingTiles.do","listSaleDTO",listSaleDTO);
			request.setAttribute("page", page);
		}
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		
		return mv;
	}
	@RequestMapping("searchProductByProductName.do")
	public ModelAndView searchProductListByProductName(String productName){
		ModelAndView mv = null;
		try {
			ArrayList<ProductDTO> productList = service.selectProductListByName(productName);
			mv = new ModelAndView("productListForSearchTiles.do","productList",productList);
			
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("searchProductByPrice.do")
	public ModelAndView searchProductListByPrice(int start, int end){
		ModelAndView mv = null;
		try {
			ArrayList<ProductDTO> productList = service.selectProductListByPrice(start, end);
			mv = new ModelAndView("productListTiles.do","productList",productList);
			
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("searchProductByCompany.do")
	public ModelAndView searchProductListByCompany(String companyId){
		ModelAndView mv = null;
		try {
			ArrayList<ProductDTO> productList = service.selectProductListByCompany(companyId);
			mv = new ModelAndView("productListTiles.do","productList",productList);
			
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("searchProductToSale.do")
	public ModelAndView searchProductListToSale(String product_Name ){
		ModelAndView mv = null;
		try {
			ArrayList<ProductDTO> productList = service.selectProductListByName(product_Name);
			mv = new ModelAndView("productListTiles.do","productList",productList);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		
		return mv;
	}
	@RequestMapping("searchProductInput.do")
	public ModelAndView searchProductInputForm(int productId){
		ModelAndView mv = null;
		
		try {
			ProductDTO pdto = service.selectProductByProductId(productId);
			mv = new ModelAndView("productSaleInputTiles.do","pdto",pdto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return  mv;
	}
	
	
}
